package com.iwuke.dance.wallet.e_coin;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.activity.WebViewActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.LogUtil;
import com.iwuke.dance.util.SharedPreferencesUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.WeChatPayUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.iwuke.dance.wallet.bean.ECoinPurposeBean;
import com.iwuke.dance.wallet.bean.ECoinTopUpBean;
import com.iwuke.dance.wallet.bean.RechargeEBalanceBean;
import com.iwuke.dance.wallet.e_coin.adapter.ECoinPurposeAdapter;
import com.iwuke.dance.wallet.e_coin.adapter.ECoinTopUpAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/17
 * 我的E币
 */
public class MyECoinActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private TextView tv_e_coin;
    private RecyclerView recycler_view;
    private TextView tv_top_up;
    private String ecurrency="0";
    private List<ECoinTopUpBean> lists = new ArrayList<>();;

    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(MyECoinActivity.this,networkEvent,title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_my_e_coin;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);
        //当前E币
        tv_e_coin = findViewById(R.id.tv_e_coin);
        //充值
        tv_top_up = findViewById(R.id.tv_top_up);

        //列表
        recycler_view = findViewById(R.id.recycler_view);


        title_bar.setTitleText("我的E币");
        title_bar.setRightText("E币明细");
        title_bar.setRightVisibility(true);
        title_bar.setOnRightListener(this);
        title_bar.setLeftOnClick(this);

        tv_top_up.setOnClickListener(this);


    }

    @Override
    protected void initData() {

        ecurrency = getIntent().getStringExtra("text");

        //当前E币
        tv_e_coin.setText(ecurrency);

        List<ECoinPurposeBean> list = new ArrayList<>();

        ECoinPurposeBean eCoinPurposeBean1 = new ECoinPurposeBean();
        eCoinPurposeBean1.item_image = R.mipmap.img_gift;
        eCoinPurposeBean1.item_title = "直播间送礼物";
        eCoinPurposeBean1.desc_title = "赠送喜欢的主播";

        ECoinPurposeBean eCoinPurposeBean2 = new ECoinPurposeBean();
        eCoinPurposeBean2.item_image = R.mipmap.img_e_coin_money;
        eCoinPurposeBean2.item_title = "活动打赏";
        eCoinPurposeBean2.desc_title = "支持喜爱的创\n作者";

        ECoinPurposeBean eCoinPurposeBean3 = new ECoinPurposeBean();
        eCoinPurposeBean3.item_image = R.mipmap.img_reward;
        eCoinPurposeBean3.item_title = "艺术圈打赏";
        eCoinPurposeBean3.desc_title = "打赏朋友和动态";

        ECoinPurposeBean eCoinPurposeBean4 = new ECoinPurposeBean();
        eCoinPurposeBean4.item_image = R.mipmap.img_more;
        eCoinPurposeBean4.item_title = "期待更多";
        eCoinPurposeBean4.desc_title = "新用途敬请期待";
        list.add(eCoinPurposeBean1);
        list.add(eCoinPurposeBean2);
        list.add(eCoinPurposeBean3);
        list.add(eCoinPurposeBean4);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);

        recycler_view.setLayoutManager(gridLayoutManager);

        ECoinPurposeAdapter eCoinPurposeAdapter = new ECoinPurposeAdapter(this, R.layout.adapter_e_coin_purpose, list);
        recycler_view.setAdapter(eCoinPurposeAdapter);

        getRechargeConfig();

    }

    private void getRechargeConfig() {
        Map<String, String> map = new HashMap<>();
        map.put("type","1");
        PathUrl.getRechargeConfig(map, new PathUrl.DataCallBackList<ECoinTopUpBean>() {
            @Override
            public void onSucessList(List<ECoinTopUpBean> list) {
                lists = list;
            }

            @Override
            public void onFail(String errorCode) {
                ToastUtil.setToast(MyECoinActivity.this,errorCode);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }else if (v.getId()==R.id.tv_top_up){
            setPopupWindow();
        }else if (v.getId()==R.id.tv_right){
            //Details详情  details
            Util.setIntent(MyECoinActivity.this, ECoinDetailsActivity.class,ecurrency);
            finish();
        }
    }

    private void setPopupWindow(){
        View view = LayoutInflater.from(MyECoinActivity.this).inflate(R.layout.popup_window_e_coin_top_up, null);
        final PopupWindow popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置位置
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        RelativeLayout rlt_popup_window_bg = view.findViewById(R.id.rlt_popup_window_bg);
        LinearLayout llt_popup_window = view.findViewById(R.id.llt_popup_window);
        TextView tv_e_coin_balance = view.findViewById(R.id.tv_e_coin_balance);//e币余额
        RecyclerView recycler_view = view.findViewById(R.id.recycler_view);//列表
        TextView button_top_up = view.findViewById(R.id.button_top_up);//立即充值

        TextView tv_e_coin_purchase_agreement = view.findViewById(R.id.tv_e_coin_purchase_agreement);//E币购买协议
        TextView tv_safety_reminder = view.findViewById(R.id.tv_safety_reminder);//安全提示

        //当前E币
        tv_e_coin_balance.setText("余额："+ecurrency+"E币");

        ECoinTopUpBean eCoinTopUpBean1 = new ECoinTopUpBean();
        eCoinTopUpBean1.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean1.e_coin = 10;
        eCoinTopUpBean1.top_up_amount = 1;

        ECoinTopUpBean eCoinTopUpBean2 = new ECoinTopUpBean();
        eCoinTopUpBean2.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean2.e_coin = 60;
        eCoinTopUpBean2.top_up_amount = 6;

        ECoinTopUpBean eCoinTopUpBean3 = new ECoinTopUpBean();
        eCoinTopUpBean3.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean3.e_coin = 300;
        eCoinTopUpBean3.top_up_amount = 30;

        ECoinTopUpBean eCoinTopUpBean4 = new ECoinTopUpBean();
        eCoinTopUpBean4.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean4.e_coin = 980;
        eCoinTopUpBean4.top_up_amount = 98;

        ECoinTopUpBean eCoinTopUpBean5 = new ECoinTopUpBean();
        eCoinTopUpBean5.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean5.e_coin = 2980;
        eCoinTopUpBean5.top_up_amount = 298;

        ECoinTopUpBean eCoinTopUpBean6 = new ECoinTopUpBean();
        eCoinTopUpBean6.item_image = R.mipmap.img_e_coin;
        eCoinTopUpBean6.e_coin = 5180;
        eCoinTopUpBean6.top_up_amount = 518;

        if (lists.size()==0) {
            lists.add(eCoinTopUpBean1);
            lists.add(eCoinTopUpBean2);
            lists.add(eCoinTopUpBean3);
            lists.add(eCoinTopUpBean4);
            lists.add(eCoinTopUpBean5);
            lists.add(eCoinTopUpBean6);
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        recycler_view.setLayoutManager(gridLayoutManager);
        ECoinTopUpAdapter eCoinTopUpAdapter = new ECoinTopUpAdapter(this, R.layout.adapter_e_coin_top_up, lists);
        recycler_view.setAdapter(eCoinTopUpAdapter);

        rlt_popup_window_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        llt_popup_window.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        eCoinTopUpAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                eCoinTopUpAdapter.setImgList(position);
                button_top_up.setText("立即充值"+lists.get(position).top_up_amount+"元");
            }
        });

        //充值
        button_top_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random random = new Random();
                int randomInt = random.nextInt(1000000000)+1000000000;
                Map<String,Object> map = new HashMap<>();
                map.put("userId",SharedPreferencesUtil.getInstance().getUserId());
                map.put("orderNo",String.valueOf(randomInt));
                map.put("amount",0.01);
                map.put("description","E币购买");
                map.put("attach","E币充值");
                map.put("goodsTag","优惠券");
                map.put("rechargeLocation","钱包");
                map.put("remark","充值");

                PathUrl.rechargeEBalance(map, new PathUrl.DataCallBack<RechargeEBalanceBean>() {
                    @Override
                    public void onSucess(RechargeEBalanceBean bean) {
                        LogUtil.logD("充值E币数据", new Gson().toJson(bean));
                        if (bean!=null){
                            WeChatPayUtil.startPayment(MyECoinActivity.this, bean.appid, bean.partnerid,
                                    bean.prepayid, bean.packageX, bean.noncestr,bean.timestamp,bean.sign);
                        }

                    }

                    @Override
                    public void onFail(String errorCode) {
                        ToastUtil.setToast(MyECoinActivity.this,errorCode);
                    }
                });

//                String sign= "W5X9XS4SwxXZRutxASr7uSNKPmbyZ0GHlDBw4pHxw6dFUtTZUhUbdvZJCzwCiTxoUIapmJKyoq1LCIKBBCYkqVp+D5xwQyZ+v/3K15ft/ZAH+qUH/VjY8JdbLZls1yLYeWlAFGoCLEvRhmhEAqIZkSl/c/gm4MqYP6V0WNaTVWGs4dFOJTNyHE9yKtON+Mnadnn251JckdWb1Lass2iWAz3pp2QfM6ufaqS+kmhUboTMDFOvpT9285ovBOEtQDjvM/fVLSDwArQxPGSzUrinUKIv63yYrvtaBJOrCNgoGUYGPFoC9+b+EKAGIBTebERjM9/k/n/o/Cs7bRyzuJVSbg==";

//                WeChatPayUtil.startPayment(MyECoinActivity.this, WXPayUtils.APP_ID,"1640802815","wx151545433276292c9c22f9cd640b120000",
//                        "Sign=WXPay", "7813e8b965b6412f93059579f74b68e9","1758165042",sign);

                //取消
//                popupWindow.dismiss();
            }
        });

        tv_e_coin_purchase_agreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //E币购买协议
                Util.setIntent(MyECoinActivity.this, WebViewActivity.class,"");
            }
        });

        tv_safety_reminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //安全提示
                Util.setIntent(MyECoinActivity.this, WebViewActivity.class,"");
            }
        });

    }
}
